package com.dbapp.uilib.widget.titlebar;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.dbapp.uilib.R;
import com.dbapp.uilib.entity.uiMenuItem;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * Created by Arirus on 2017/7/27.
 */

public class ListMenu extends PopupWindow {
  public interface OnMenuItemClickListener {
    void onClick(int index);
  }

  private RecyclerView mRecyclerView;
  private RecyclerAdapter mAdapter;
  private List<uiMenuItem> items;

  public ListMenu(ArrayList<uiMenuItem> list, View contentView, boolean focusable) throws Exception {
    super(contentView, WRAP_CONTENT, WRAP_CONTENT, focusable);
    mRecyclerView = (RecyclerView) contentView.findViewById(R.id.menu_recycler);
    if (mRecyclerView == null) throw new Exception("没有找到Recycler控件");

    items = list;
    mAdapter = new RecyclerAdapter(contentView.getContext(), items, R.layout.menu_item);
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(contentView.getContext()));
  }

  public void setOnItemClickListener(OnMenuItemClickListener clickListener) {
    mAdapter.setOnItemClickListener(clickListener);
  }

  static private class RecyclerViewHolder extends RecyclerView.ViewHolder {

    private TextView textView;
    private SimpleDraweeView imageView;
    private RecyclerAdapter mAdapter;

    public RecyclerViewHolder(View itemView, Context context, RecyclerAdapter adapter) {
      super(itemView);
      textView = (TextView) itemView.findViewById(R.id.menu_item_txt);
      imageView = (SimpleDraweeView) itemView.findViewById(R.id.menu_item_img);
      mAdapter = adapter;
      itemView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          if (mAdapter != null && mAdapter.getListener()!=null) {
            mAdapter.getListener().onClick(getLayoutPosition());
          }
        }
      });
    }

    private void bind(uiMenuItem s) {
      if (s.getName() != null) {
        textView.setVisibility(View.VISIBLE);
        textView.setText(s.getName());
      }else
        textView.setVisibility(View.GONE);

      if (s.getIcon() != null) {
        imageView.setImageURI(s.getIcon());
        imageView.setVisibility(View.VISIBLE);
      }else {
        imageView.setVisibility(View.GONE);
      }
    }
  }

  static private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {

    private Context context;
    private List<uiMenuItem> strings;
    private int resID;
    private OnMenuItemClickListener mListener;

    public RecyclerAdapter(Context context, List<uiMenuItem> strings, int resID) {
      this.context = context;
      this.strings = strings;
      this.resID = resID;
    }

    @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(context).inflate(resID, parent, false);
      RecyclerViewHolder viewHolder = new RecyclerViewHolder(view, context, this);
      return viewHolder;
    }

    @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) {
      holder.bind(strings.get(position));
    }

    public void setOnItemClickListener(OnMenuItemClickListener listener) {
      mListener = listener;
    }

    public OnMenuItemClickListener getListener() {
      return mListener;
    }

    @Override public int getItemCount() {
      return strings.size();
    }
  }
}
